package view;

import controller.UserController;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.SQLException;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

public class UserPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Listener
	UserController userController;
	// GUI component
	JLabel userNameLabel = new JLabel("username:");
	JLabel passwordLabel = new JLabel("password:");
	JLabel loginedUserLabel = new JLabel();
	JTextField userNameField = new JTextField(15);
	JPasswordField passwordField = new JPasswordField(15);
	JCheckBox autoLoginCheckBox = new JCheckBox("Auto login next time!");
	JButton loginButton = new JButton("Login");
	JButton newButton = new JButton("New");
	JButton deleteButton = new JButton("Delete");
	JButton modifyButton = new JButton("Modify");

	TaskFrame taskFrame;

	public UserPanel(TaskFrame taskFrame) throws SQLException {

		this.taskFrame = taskFrame;
		userController = new UserController(this);

		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		// userNameLabel
		gbc.gridx = 0;
		gbc.gridy = 0;
		this.add(userNameLabel, gbc);
		// passwordLabel
		gbc.gridx = 0;
		gbc.gridy = 1;
		this.add(passwordLabel, gbc);
		// userNameField
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.anchor = GridBagConstraints.WEST;
		this.add(userNameField, gbc);
		// passwordField
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		this.add(passwordField, gbc);
		// autoLoginCheckbox
		autoLoginCheckBox.setEnabled(false);
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 3;
		gbc.anchor = GridBagConstraints.CENTER;
		this.add(autoLoginCheckBox, gbc);
		// loginButton
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(4, 4, 4, 4);
		this.add(loginButton, gbc);
		// loginUserLabel
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.gridwidth = 3;
		this.add(loginedUserLabel, gbc);
		// newButton
		gbc.gridwidth = 1;
		gbc.gridx = 2;
		gbc.gridy = 5;
		gbc.insets = new Insets(4, 4, 4, 4);
		this.add(newButton, gbc);
		// deleteButton
		gbc.gridx = 0;
		gbc.gridy = 5;
		this.add(deleteButton, gbc);
		// modifyButton
		gbc.gridx = 1;
		gbc.gridy = 5;
		this.add(modifyButton, gbc);

		loginButton.addActionListener(userController);
		newButton.addActionListener(userController);
		deleteButton.addActionListener(userController);
		autoLoginCheckBox.addActionListener(userController);
		modifyButton.addActionListener(userController);

		init();
	}

	public JCheckBox getAutoLoginCheckBox() {
		return autoLoginCheckBox;
	}

	public JButton getDeleteButton() {
		return deleteButton;
	}

	public JButton getLoginButton() {
		return loginButton;
	}

	// modle

	public JLabel getLoginedUserLabel() {
		return loginedUserLabel;
	}

	public JButton getModifyButton() {
		return modifyButton;
	}

	public JButton getNewButton() {
		return newButton;
	}

	@SuppressWarnings("deprecation")
	public String getPassword() {
		return passwordField.getText();
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public JLabel getPasswordLabel() {
		return passwordLabel;
	}

	public TaskFrame getTaskFrame() {
		return taskFrame;
	}

	public UserController getUserControllor() {
		return userController;
	}

	public String getUserName() {
		return userNameField.getText();
	}

	public JTextField getUserNameField() {
		return userNameField;
	}

	public JLabel getUserNameLabel() {
		return userNameLabel;
	}

	public void init() {
		loginedUserLabel.setText("");
		deleteButton.setEnabled(false);
		autoLoginCheckBox.setEnabled(false);
	}

	public boolean isAutoLogin() {
		return autoLoginCheckBox.isSelected();
	}

	public void setAutoLogin(boolean b) {
		this.autoLoginCheckBox.setSelected(b);
	}

	public void setAutoLoginCheckBox(JCheckBox autoLoginCheckBox) {
		this.autoLoginCheckBox = autoLoginCheckBox;
	}

	public void setDeleteButton(JButton deleteButton) {
		this.deleteButton = deleteButton;
	}

	public void setLoginButton(JButton loginButton) {
		this.loginButton = loginButton;
	}

	public void setLoginedUserLabel(JLabel loginedUserLabel) {
		this.loginedUserLabel = loginedUserLabel;
	}

	public void setModifyButton(JButton modifyButton) {
		this.modifyButton = modifyButton;
	}

	public void setNewButton(JButton newButton) {
		this.newButton = newButton;
	}

	public void setPassword(String pwd) {
		this.passwordField.setText(pwd);
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	public void setPasswordLabel(JLabel passwordLabel) {
		this.passwordLabel = passwordLabel;
	}

	public void setTaskFrame(TaskFrame taskFrame) {
		this.taskFrame = taskFrame;
	}

	public void setUserControllor(UserController userControllor) {
		this.userController = userControllor;
	}

	public void setUserName(String name) {
		this.userNameField.setText(name);
	}

	public void setUserNameField(JTextField userNameField) {
		this.userNameField = userNameField;
	}

	public void setUserNameLabel(JLabel userNameLabel) {
		this.userNameLabel = userNameLabel;
	}

}